package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Jialin
 * @create 2023-06-21 22:16
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee queryEmployee = employeeService.getOne(queryWrapper);

        if (queryEmployee == null) return R.error("登录失败: 账号不存在");
        if (!queryEmployee.getPassword().equals(password)) return R.error("登录失败: 密码错误");
        if (queryEmployee.getStatus() == 0) return R.error("登录失败: 账户已禁用");

        request.getSession().setAttribute("employee", queryEmployee.getId());
        return R.success(queryEmployee);
    }

    /**
     * 员工退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 员工添加
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getIdNumber().substring(12).getBytes()));
        employeeService.save(employee);
        return R.success("员工添加成功");
    }

    /**
     * 员工信息分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(int page, int pageSize, String name) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(name), Employee::getName, name).orderByDesc(Employee::getUpdateTime);
        return R.success(employeeService.page(employeePage, queryWrapper));
    }

    /**
     * 员工信息修改
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        employee.setPassword(employee.getPassword() == null ? null : DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 显示员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getEmployeeById(@PathVariable Long id) {
        return R.success(employeeService.getById(id));
    }


}
